package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.other.FileDto;
import com.dg.cuckoo.admin.dto.projectInfo.ProViewDto;
import com.dg.cuckoo.admin.repository.FileRepository;
import com.dg.cuckoo.admin.repository.ProViewRepository;
import com.dg.cuckoo.admin.service.ProjectInfoServer;
import com.dg.cuckoo.admin.service.ProjectOverviewService;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectOverviewServiceimpl implements ProjectOverviewService {

    @Autowired
    private ProViewRepository repository;
    @Autowired
    private FileRepository fileRepository;
    @Autowired
    private ProjectInfoServer projectInfoServer;

    @Transactional
    @Override
    public void save(String projectId,ProViewDto dto, MultipartFile[] address, MultipartFile[] features) throws Exception {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        checkSave(dto);
        dto.setProjectId(projectId);
        ProViewDto save = repository.save(dto);

        if(address!=null){
            projectInfoServer.powerfulFileSave(address,save.getId());
        }

        if(features!=null){
            projectInfoServer.powerfulFileSave(features,save.getId());
        }
    }

    @Override
    public String byProjectId(String projectId) {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        ProViewDto dto = repository.findByProjectId(projectId);
        List<FileDto> address = fileRepository.findByByMark(projectId);
        List<FileDto> features = fileRepository.findByByMark(projectId);
        JSONObject json = new JSONObject();
        json.put("dto",dto);
        json.put("address",address);
        json.put("features",features);
        return json.toString();
    }

    @Transactional
    @Override
    public void update(ProViewDto dto, MultipartFile[] address, MultipartFile[] features, String projectId) throws Exception {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        checkSave(dto);
        dto.setProjectId(projectId);
        repository.save(dto);

        if(address!=null){
            List<FileDto> byByMarkAndType = fileRepository.findByByMark(projectId);
            fileRepository.deleteAll(byByMarkAndType);
            projectInfoServer.powerfulFileSave(address,projectId);
        }

        if(features!=null){
            List<FileDto> byByMarkAndType = fileRepository.findByByMark(projectId);
            fileRepository.deleteAll(byByMarkAndType);
            projectInfoServer.powerfulFileSave(features,projectId);
        }
    }

    @Override
    public List<String> address(String projectId) {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        List<FileDto> file = fileRepository.findByByMark(projectId);
        List<String> list=new ArrayList<>();
        for(FileDto f:file){
            list.add(f.getFileUrl());
        }
        return list;
    }


    public void checkSave(ProViewDto dto){

    }
}
